//一般直接写在一个js文件中
layui.use(['element','form','layer','laytpl','laydate'],function () {
    var form = layui.form,
    layer = layui.layer,
        laytpl = layui.laytpl,
        laydate = layui.laydate;
    laydate.render({
        elem: '#endTime' //指定元素
    });

    //选择老人
    $("#elderlyName").on('click',function () {
        layer.open({
            type: 2,
            content: '/receipt/select/elderly',
            area: ['800px', '600px'],
            title:'选择老人',
            btn: ['确定 '],
            yes:function(index, layero){
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
                var checkStatus = iframeWin.layui.table.checkStatus('mytable');
                if(checkStatus.data.length < 1 || checkStatus.data.length > 1){
                    iframeWin.layui.layer.msg("请选择一位老人");
                    return false;
                }
                var name = checkStatus.data[0].name;
                var id = checkStatus.data[0].id;
                var code = checkStatus.data[0].code;
                $("#elderlyName").val(name);
                $("#elderlyId").val(id);
                $("#elderlyCode").val(code);
                //获取老人为付款的服务记录
                getRecords(id);
                layer.close(index);
            }
        });
    });

    //获取老人未付款的服务记录
    function getRecords(elderlyId){
        $.ajax({
            url:"/receipt/elderly/records",
            data:{elderlyId: elderlyId},
            type:"post",
            dataType:'json',
            success:function (rs) {
                if(rs.code == 200){
                    var getTpl=document.getElementById('fwxm').innerHTML;
                    var $recordsTbody = $("#records_tbody");
                    $recordsTbody.empty();
                    if(rs.data.length > 0){
                        $.each(rs.data,function (i,d) {
                            d.createdTime = $.dateFormat(new Date(d.createdTime),'yyyy-MM-dd');
                            laytpl(getTpl).render(d, function(html){
                                $recordsTbody.append( html);
                            });
                        });
                        form.render('checkbox'); //刷新select选择框渲染
                        calcMonery();
                        form.on('checkbox', function(data){
                            calcMonery();
                        });
                    }else {
                        $recordsTbody.append('<tr><td style="text-align: center;" colspan="4">暂无未收费服务项目</td></tr>')
                    }

                }
            },
            error:function (rs) {

            }
        });
    }

    //添加收费项目
    $("#addRow").on('click',function(){
        var getTpl=document.getElementById('qtsf').innerHTML;
        var $itemsTbody = $("#items_tbody");
        laytpl(getTpl).render({}, function(html){
            $itemsTbody.append( html);
        });
    });
});

//删除其它收费项目
function delRow(that) {
    $(that).closest('tr').remove();
    calcMonery();
}

//计算费用
function calcMonery() {
    var $cost = $(".cost");
    var total_cost = 0; //服务项目总费用
    if($cost.size() > 0){
        $cost.each(function(){
            var $recordsId = $(this).closest('tr').find(".recordsId");
            if($recordsId.prop("checked")){
                var cost = $(this).text();
                total_cost += Number(cost);
            }
        });
    }
    var $monery = $(".monery");
    var total_monery = 0; //其它收费项目总费用
    if($monery.size() > 0){
        $monery.each(function(){
            var monery = $(this).val();
            if(monery){
                total_monery += Number(monery);
            }
        });
    }

    var amount = total_cost + total_monery; // 总收费金额
    $("#amount").val(amount);
    $("#qtze").val(total_monery);
    $("#fwze").val(total_cost);


}